#include <iostream>
#include <vector>
#include <set>
#include <sstream>
#include <algorithm>

int main()
{
    std::string s;
    std::getline(std::cin, s);
    std::istringstream iss(s);

    std::set<int> nums;
    int n;
    std::string c;
    while (std::getline(iss, c, ','))
    {
        n = std::stoi(c);
        if (n < 1 || n > 9)
        {
            std::cout << -1 << std::endl;
            return 0;
        }
        if (n == 9)
        {
            nums.insert(6);
        }
        else if (n == 5)
        {
            nums.insert(2);
        }
        else
        {
            nums.insert(n);
        }
    }

    if (nums.size() < 4)
    {
        std::cout << -1 << std::endl;
        return 0;
    }

    std::vector<int> res_arr(nums.begin(), nums.end());
    if (nums.count(2))
    {
        res_arr.push_back(5);
    }
    if (nums.count(6))
    {
        res_arr.push_back(9);
    }

    std::vector<int> num_arr(nums.begin(), nums.end());
    int N = *std::max_element(num_arr.begin(), num_arr.end());
    for (int i = 0; i < num_arr.size(); i++)
    {
        for (int j = 0; j < num_arr.size(); j++)
        {
            if (i == j)
                continue;
            res_arr.push_back(num_arr[i] * 10 + num_arr[j]);
            if (num_arr[i] == 2)
            {
                res_arr.push_back(5 * 10 + num_arr[j]);
            }
            else if (num_arr[i] == 6)
            {
                res_arr.push_back(9 * 10 + num_arr[j]);
            }
            else
            {
                if (num_arr[j] == 2)
                    res_arr.push_back(num_arr[i] * 10 + 5);
                else if (num_arr[j] == 6)
                    res_arr.push_back(num_arr[i] * 10 + 9);
            }
        }
    }

    std::sort(res_arr.begin(), res_arr.end(), std::less<int>());
    std::cout << res_arr[N - 1] << std::endl;

    return 0;
}